﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Cocbook_DAL;
using Cocbook_DTO;

namespace Cocbook___modul_sale
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        public int check = 0;
        public Staff s = new Staff();
        public StaffDAL sd = new StaffDAL();


        /// <summary>
        /// Kiểm tra đăng nhập
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Pass"></param>
        /// <returns></returns>
        public Boolean CheckLogin(string ID, string Pass)
        {
            s = sd.GetStaffByUserName(ID.ToLower());
            if (s != null && (Pass == s.Password) && (s.Position == "seller" || s.Position == "admin"))
            {
                frmMain.staff = s;
                return true;
            }
            else
            {
                return false;
            }
        }
        

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (CheckLogin(txtID.Text, txtPass.Text))
            {
                check = 1;
                frmMain frm = new frmMain();
                this.Visible = false;
                frmMain.status = true;
                frm.ShowDialog();
                this.Visible = true;
                txtID.Clear();
                txtPass.Clear();
                txtID.Focus();
            }
            else
            {
                txtPass.Text = "";
                txtID.Text = "";
                txtID.Focus();
                MessageBox.Show("Invalid id or password.\n\ror\n\rYour possition is not a seller.\n\r\n\rPlease try again!","Login error",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (check == 0)
            {
                Application.Exit();
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter)
                buttonX1_Click(null, null);
            return base.ProcessDialogKey(keyData);
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            Connection con = new Connection();
            if (con.CheckConnection())
            {
            }
            else
            {
                MessageBox.Show("Không thể kết nối với Server", "Connect Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                frmSetupServer setS = new frmSetupServer();
                setS.ShowDialog();
            }
        }
    }
}